//201902104003邵潇漩
package bysj.service;

import bysj.dao.GraduateProjectDao;
import bysj.domain.GraduateProject;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectService {
	private static GraduateProjectDao graduateProjectDao= GraduateProjectDao.getInstance();
	private static GraduateProjectService graduateProjectService=new GraduateProjectService();
	private GraduateProjectService(){}

	public static GraduateProjectService getInstance(){
		return graduateProjectService;
	}

	public Collection<GraduateProject> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<GraduateProject> graduateProjects =null;
		graduateProjects = graduateProjectDao.findAll(pagination,conditionList, connection);
		//关闭连接
		JdbcHelper.close(connection);
		return graduateProjects;
	}

	public Collection<GraduateProject> findAll() throws SQLException{
		Connection connection=JdbcHelper.getConn();
		Collection<GraduateProject> graduateProjects =null;
		graduateProjects = graduateProjectDao.findAll(connection);
		//关闭连接
		JdbcHelper.close(connection);
		return graduateProjects;
	}
	public GraduateProject find(Integer id) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		GraduateProject graduateProject=null;
		graduateProject=graduateProjectDao.find(id,connection);
		//关闭连接
		JdbcHelper.close(connection);
		return graduateProject;
	}

	public boolean update(GraduateProject graduateProject) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		boolean graduateProject_update=false;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			graduateProject_update=graduateProjectDao.update(graduateProject,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return graduateProject_update;
	}

	public boolean add(GraduateProject graduateProject) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		boolean graduateProject_add=false;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			graduateProject_add=graduateProjectDao.add(graduateProject,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return graduateProject_add;
	}

	public boolean delete(Integer id) throws SQLException {
		//获得连接
		Connection connection = JdbcHelper.getConn();
		this.find(id);
		boolean graduateProject_del = false;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			graduateProject_del = graduateProjectDao.delete(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return graduateProject_del;
	}
}
